package com.heima.alarm.controller;

import com.heima.redis.template.RedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("unlock")
public class RedisUnlockController {

    @Autowired
    RedisRepository redisRepository;

    private static Object obj = null;


    static {
        obj = RedisUnlockController.class;
    }


    @Autowired
    Environment env;

    @GetMapping
    public String unCount() {

        synchronized (obj) {
            int incr = (int) redisRepository.get("count:11111");

            if (incr > 0) {
                incr = incr - 1;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                redisRepository.set("count:11111", incr);


                System.out.println(env.getProperty("server.port") + "--当前商品库存：" + incr);
            } else {
                System.out.println(env.getProperty("server.port") + "当前商品----库存不足-----" + incr);
            }
        }

        return env.getProperty("server.port") + " SUCCESS";
    }


    @GetMapping("key")
    public void setKey() {
        redisRepository.set("count:11111", 100);
    }

}
